﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace SocialLearning
{
    public partial class GroupManagerForm : Form
    {
        #region Private Fields

        private UAC_GroupManagement gm = new UAC_GroupManagement();
        private IList<string> group;

        #endregion

        #region Constructor

        /// <summary>
        /// Initializes a new instance of the <see cref="GroupManagerForm"/> class.
        /// </summary>
        public GroupManagerForm()
        {
            InitializeComponent();
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Handles the Load event of the GroupManagerForm control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void GroupManagerForm_Load(object sender, EventArgs e)
        {
            LoadCbxData();
        }

        /// <summary>
        /// Loads the CBX data.
        /// </summary>
        private void LoadCbxData()
        {
            List<string> groupList = gm.SeeGroup().ToList();
            foreach (string groupname in groupList)
            {
                cbxUpdateGroup.Items.Add(groupname);
                cbxDeleteGroup.Items.Add(groupname);
            }
        }

        /// <summary>
        /// Creates the group.
        /// </summary>
        /// <param name="name">The name.</param>
        private void CreateGroup(string name)
        {
            try
            {
                gm.Create(name);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// Updates the group.
        /// </summary>
        /// <param name="oldName">The old name.</param>
        /// <param name="newName">The new name.</param>
        private void UpdateGroup(string oldName, string newName)
        {
            try
            {
                gm.UpdateGroup(oldName, newName);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// Adds the member.
        /// </summary>
        /// <param name="groupname">The groupname.</param>
        /// <param name="userid">The userid.</param>
        private void AddMember(string groupname, string userid)
        {
            try
            {
                gm.AddMember(groupname, userid);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// Removes the group.
        /// </summary>
        /// <param name="name">The name.</param>
        private void DeleteGroup(string name)
        {

        }

        /// <summary>
        /// Initializes the group.
        /// </summary>
        private void InitializeGroup()
        {
            //Remove an entire group
            //testGroup.RemoveGroup("Group1");

            //Remove group member
            //testGroup.RemoveGroupMember("1001");

            //Update group name
            //testGroup.UpdateGroup("Group1", "Group6");

            //See all the members in a group
            Console.WriteLine("All group members");
            group = gm.SeeGroupMembers("Group1");

            if (group != null)
            {
                foreach (var VARIABLE in group)
                {
                    Console.WriteLine(VARIABLE);
                }
            }
            //Console.WriteLine();
            //Console.WriteLine();

            //Check if groups exist
            //Console.WriteLine("Check if groups exist");
            //Console.WriteLine(testGroup.SearchGroup("Group1"));
            //Console.WriteLine(testGroup.SearchGroup("Group3"));
            //Console.WriteLine();
            //Console.WriteLine();

            //Search member in groups
            //Console.WriteLine("Groups that members 1001 exist");
            group = gm.SearchGroupMember("1001");

            if (group != null)
            {
                foreach (var VARIABLE in group)
                {
                    //Console.WriteLine(VARIABLE);
                }
            }
            //Console.ReadLine();
        }

        #endregion

        #region Private Controls

        /// <summary>
        /// Handles the Click event of the btnAdd control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txtCreateName.Text))
                MessageBox.Show("Please enter a group name!", "Information",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
            else
                if (gm.SearchGroup(txtCreateName.Text))
                    CreateGroup(txtCreateName.Text);
                else
                    MessageBox.Show("Group already exists! Please try again", "Information",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
            txtCreateName.Text = string.Empty;
        }

        /// <summary>
        /// Handles the Click event of the btnUpdate control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txtUpdateName.Text))
                MessageBox.Show("Please enter a group name!", "Information",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
            else
                UpdateGroup(cbxUpdateGroup.SelectedItem.ToString(), txtUpdateName.Text);
        }

        /// <summary>
        /// Handles the Click event of the btnDelete control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(cbxDeleteGroup.SelectedValue.ToString()))
                MessageBox.Show("Please enter a group name!", "Information",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
            else
                DeleteGroup(cbxDeleteGroup.SelectedItem.ToString());
        }

        /// <summary>
        /// Handles the Click event of the btnClose control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        #endregion
    }
}
